// pages/treasurebox/treasurebox.js
const app = getApp();// 云：add this before every page //
Page({

  /**
   * 页面的初始数据
   */
  data: {
    safeHeight: app.globalData.tabBarHeight + app.globalData.safeBottom, // 云：add this before every page //
    safeTop: app.globalData.safeTop,
    dataList:[],
    category:[
        '推荐','购物','生活','出行'
    ],
    treasurebox:{
      tj:[
        {
          id:1,
          src: '/image/treasurebox/1-xmly.png',
          text: '喜马拉雅',
          appId: 'wxb63203ca8ecbc8fc'
        },
        {
          id:2,
          src: '/image/treasurebox/2-aqy.png',
          text: '爱奇艺',
          appId: 'wxcd10170e55a1f55d'
        }  
      ],
      gw:[
        {
          id:3,
          src: '/image/treasurebox/3-pdd.png',
          text: '拼多多',
          appId: 'wx32540bd863b27570'

        },
        {
          id:4,
          src: '/image/treasurebox/4-xmsc.png',
          text: '小米商场',
          appId: 'wx17ea87763491620f'
        }
      ],
      sh:[
        {
          id:5,
          src: '/image/treasurebox/5-cnyz.png',
          text: '菜鸟驿站',
          appId: 'wx795bd500b21f8fee'
        },
        {
          id:6,
          src: '/image/treasurebox/6-xj.png',
          text: '携程',
          appId: 'wx0e6ed4f51db9d078'
        },
        {
          id:7,
          src: '/image/treasurebox/7-sh.png',
          text: '搜狐',
          appId: 'wxa1255d2a5b4291d3'
        },
        {
          id:8,
          src: '/image/treasurebox/8-ks.png',
          text: '快手',
          appId: 'wx79a83b1a1e8a7978'
        }
      ],
      cx:[
        {
          id:9,
          src: '/image/treasurebox/9-12323.png',
          text: '12123',
          appId: "wx49a80525eebd2583"
        },
        {
          id:10,
          src: '/image/treasurebox/10-gddt.png',
          text: '高德地图',
          appId: 'wxbc0cf9b963bd3550'
        }
      ]

    }
  },

  tapToMiniprogram(e){
    console.log(e.currentTarget.dataset)
     wx.navigateToMiniProgram({
           appId: e.currentTarget.dataset.appid,
           path: '',
           envVersion: 'release',// 打开正式版
           success(res) {
                // 打开成功
           },
           fail: function (err) {
             console.log(err);
          }
       })
  },

  init(){
    wx.showLoading({
    title: '正在加载',
    mask: true
    })
    wx.request({
    url: app.globalData.server+'/app-api/epg/miniprogram/list',
    header:{},
    method: "get",
    success: (res) => {
        console.log("获取宝箱列表成功：",res)
        let dataTemp = {}
        for(let i=0;i<res.data.data.length;i++){
        if(dataTemp.hasOwnProperty(res.data.data[i].category)){
            dataTemp[res.data.data[i].category].push(res.data.data[i])
        }
        else{
            dataTemp[res.data.data[i].category]=[res.data.data[i]]
        }
        }
        console.log("获取宝箱列表处理之后的数据是：",dataTemp)
        this.setData({
        dataList: dataTemp
        })
        
    },
    fail: (err) => {
        console.log("获取历史记录出错：",err);
    },
    complete:()=>{
        wx.hideLoading();
    }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.init()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (typeof this.getTabBar === 'function' && this.getTabBar()) {
        this.getTabBar().setData({
          selected: 3
        })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})